import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class PlatformAwareAppBar extends StatelessWidget
    implements PreferredSizeWidget {
  const PlatformAwareAppBar({
    super.key,
    this.title,
    this.showBackButton = false, // Optional: for controlling the back button
    this.onBackButtonPressed, // Optional: callback for back button
  });

  final Widget? title;
  final bool showBackButton;
  final VoidCallback? onBackButtonPressed;

  @override
  Widget build(BuildContext context) {
    if (Theme.of(context).platform == TargetPlatform.iOS) {
      return CupertinoNavigationBar(
        backgroundColor: Colors.transparent, // 关键属性
        border: null, // 移除底部边框
        // brightness: Brightness.dark, // 根据背景色调整图标颜色
        middle: title,
        leading:
            showBackButton
                ? CupertinoNavigationBarBackButton(
                  onPressed:
                      onBackButtonPressed ?? () => Navigator.of(context).pop(),
                )
                : null,
      );
    } else {
      return AppBar(
        title: title,
        automaticallyImplyLeading:
            showBackButton, // AppBar handles this automatically
      );
    }
  }

  @override
  Size get preferredSize => const Size.fromHeight(kToolbarHeight); // Standard AppBar/NavigationBar height
}
